package cn.flant.task.console;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommandParser {
	private Pattern namePattern = Pattern.compile("^(\\w+)");
	private Pattern paramPattern = Pattern.compile("-([a-zA-Z?]+)(\\s*([\\w\\W&&[^-]][\\w\\W&&[^\\s]]*))?");

	public Command parseCommand(String line) {
		Matcher m = namePattern.matcher(line);
		if (!m.find()) {
			return null;
		}
		Command cmd = new Command();
		cmd.setName(m.group(1));
		m = paramPattern.matcher(line);
		while (m.find()) {
			String opt = m.group(1);
			String value = m.group(3);
			cmd.putOptionValue(opt, value);
		}
		return cmd;
	}

}